import Vue from 'vue'
import VueRouter from 'vue-router'

//导入需要的组件
import Login from "../views/login/Login.vue"
// import Layout from "../views/layout/Layout.vue"
import Layout from "@/views/layout/Layout.vue"


const originalPush = VueRouter.prototype.push;
//重写VueRouter原型上push方法,解决自己跳转自己出错误的一个bug
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



Vue.use(VueRouter)

//配置路由
const routes = [
  //登录
  {
    path: "/login",
    component: Login

  },

  //后台首页
  {
    path: "/",//本来是App
    component: Layout,
    redirect: "/home",//重定向
    meta: { path: "/home", title: "后台首页" },
    children: [
      {
        path: "/home",
        component: () => import("../views/home/Home.vue")
      }
    ]
  },

  // 订单管理
  {
    path: "/order",
    component: Layout,
    meta: { path: "/order", title: "订单管理" },
    children: [
      {
        path: "/",
        component: () => import("../views/order/Order.vue")
      }
    ]
  },

  //商品管理
  {
    path: "/goods",
    component: Layout,
    redirect: "/goods/goods-list",
    meta: { path: "/goods", title: "商品管理" },
    children: [
      {
        path: "/goods/goods-list",
        component: () => import("../views/goods/GoodsList.vue"),
        meta: { path: "/goods/goods-list", title: "商品列表" },

      },
      {
        path: "/goods/add-goods",
        component: () => import("../views/goods/AddGoods.vue"),
        meta: { path: "/goods/goods-goods", title: "添加商品" },

      },
      {
        path: "/goods/goods-cate",
        component: () => import("../views/goods/GoodsCate.vue"),
        meta: { path: "/goods/goods-cate", title: "商品分类" },

      },
    ]
  },

  //店铺管理
  {
    path: "/shop",
    component: Layout,
    meta: { path: "/shop", title: "店铺管理" },
    children: [
      {
        path: "/",
        component: () => import("../views/shop/Shop.vue")
      }
    ]
  },
  //账号管理
  {
    path: "/acc",
    component: Layout,
    redirect: "/acc/acc-list",
    meta: { path: "/acc", title: "账号管理" },
    children: [
      {
        path: "/acc/acc-list",
        component: () => import("../views/account/AccList.vue"),
        meta: { path: "/acc/acc-list", title: "账号列表" },
      },
      {
        path: "/acc/add-acc",
        component: () => import("../views/account/AddAcc.vue"),
        meta: { path: "/acc/add-acc", title: "添加账号" },
      },
      {
        path: "/acc/password",
        component: () => import("../views/account/Password.vue"),
        meta: { path: "/acc/password", title: "修改密码" },
      },
      {
        path: "/acc/personal",
        component: () => import("../views/account/Personal.vue"),
        meta: { path: "/acc/personal", title: "个人中心" },
      },
    ]
  },
  //销售统计
  {
    path: "/total",
    component: Layout,
    redirect: "/total/goods-total",
    meta: { path: "/total", title: "销售统计" },
    children: [
      {
        path: "/total/goods-total",
        component: () => import("../views/total/GoodsTotal.vue"),
        meta: { path: "/total/goods-total", title: "商品统计" },
      },
      {
        path: "/total/order-total",
        component: () => import("../views/total/OrderTotal.vue"),
        meta: { path: "/total/order-total", title: "订单统计" },
      },
    ]
  },
]

const router = new VueRouter({
  routes
})


//全局前置守卫  在路由对象暴露之前
router.beforeEach((to,from,next)=>{
//to 到哪里去
//from 从哪里来
//next 同意 方形 通过

//判断本地储存是否有token ,有放行 没有 返回登录页面
let isLogin = localStorage.getItem("t_k")?true:false;
if(isLogin){
  //放行
  next();
}else{
  //返回登录页面
  if(to.path=="/login"){
    next();
  }else{
    // window.location.href="#/login"
    next("/login");
  }
}

})


export default router
